﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPostText.Models;

namespace MvcPostText.Controllers
{
    public class EntryController : Controller
    {
        EntryDBContext db = new EntryDBContext();

        //
        // GET: /Entry/

        public ActionResult Index()
        {
            var allEntries = from e in db.Entries 
                             select e;
            return View(allEntries.ToList());
        }

        //
        // GET: /Entry/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Entry/Create
        [HttpPost]
        public ActionResult Create(Entry newEntry)
        {
            if (ModelState.IsValid)
            {
                db.Entries.Add(newEntry);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(newEntry);
        }

        public ActionResult ListAndCreate()
        {
            EntryListAndAdd tmp = new EntryListAndAdd();
            var allEntries = from e in db.Entries 
                             select e;
            tmp.Entries = allEntries.ToList();
            tmp.NewEntry = null;

            return View(tmp);
        }

        [HttpPost]
        public ActionResult ListAndCreate(Entry newEntry)
        {
            EntryListAndAdd tmp = new EntryListAndAdd();
            var allEntries = from e in db.Entries
                             select e;
            tmp.Entries = allEntries.ToList();

            if (ModelState.IsValid)
            {
                db.Entries.Add(newEntry);
                db.SaveChanges();
                return RedirectToAction("ListAndCreate");
            }

            tmp.NewEntry = newEntry;
            return View(tmp);
        }
    }
}
